// The MIT License (MIT)

// Copyright (c) 2020 by German A. Arias

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// IUP
#include <stdlib.h>
#include <iup/iup.h>
#include <iup/iupkey.h>
#include <iup/iup_config.h>
#include <iup/iupcontrols.h>

// For IM
#include <im/im.h>
#include <im/im_image.h>
#include <iup/iupim.h>

// For CD
#include <cd/cd.h>
#include <cd/wd.h>
#include <cd/cdiup.h>
#include <cd/cdim.h>

#ifndef _library_eiffel_iup_h
#   define _library_eiffel_iup_h
#   if defined(_MSC_VER) && (_MSC_VER >= 1020)
#      pragma once
#   endif

// Convert const char* to char*
char* unconst_char (const char* s);

void set_callbacks (EIF_REFERENCE iup_obj,
         EIF_PROC launch_help_cb_fn, EIF_PROC launch_idle_action_fidle, EIF_PROC launch_map_cb_fn,
	 EIF_PROC launch_unmap_cb_fn, EIF_PROC launch_destroy_cb_fn, EIF_PROC launch_ldestroy_cb_fn,
	 EIF_PROC launch_getfocus_cb_fn, EIF_PROC launch_killfocus_cb_fn, EIF_PROC launch_enterwindow_cb_fn,
	 EIF_PROC launch_leavewindow_cb_fn, EIF_PROC launch_action_fn, EIF_PROC launch_k_any_fni,
	 EIF_PROC launch_close_cb_fn, EIF_PROC launch_copydata_cb_fnsi, EIF_PROC launch_dropfiles_cb_fnsiii,
         EIF_PROC launch_mdiactivate_cb_fn, EIF_PROC launch_move_cb_fnii, EIF_PROC launch_resize_cb_fnii,
	 EIF_PROC launch_show_cb_fni, EIF_PROC launch_trayclick_cb_fniii, EIF_PROC launch_file_cb_fnss,
	 EIF_PROC launch_colorupdate_cb_fn, EIF_PROC launch_cancel_cb_fn, EIF_PROC launch_tabchange_cb_fnnn,
	 EIF_PROC launch_tabchangepos_cb_fnii, EIF_PROC launch_tabclose_cb_fni, EIF_PROC launch_rightclick_cb_fni,
	 EIF_PROC launch_detached_cb_fnnii, EIF_PROC launch_restored_cb_fnnii, EIF_PROC launch_openclose_cb_fni,
	 EIF_PROC launch_extrabutton_cb_fnii, EIF_PROC launch_valuechanged_cb_fn, EIF_PROC launch_button_cb_fniiiis,
	 EIF_PROC launch_action_fnff, EIF_PROC launch_focus_cb_fni, EIF_PROC launch_motion_cb_fniis, EIF_PROC launch_keypress_cb_fnii,
	 EIF_PROC launch_scroll_cb_fniff, EIF_PROC launch_wheel_cb_fndiis, EIF_PROC launch_click_cb_fns,
	 EIF_PROC launch_action_fnsii, EIF_PROC launch_caret_cb_fniii, EIF_PROC launch_dblclick_cb_fnis,
	 EIF_PROC launch_dragdrop_cb_fniiii, EIF_PROC launch_dropdown_cb_fni, EIF_PROC launch_edit_cb_fnis,
	 EIF_PROC launch_multiselect_cb_fns, EIF_PROC launch_spin_cb_fni, EIF_PROC launch_action_fnis,
	 EIF_PROC launch_action_fni, EIF_PROC launch_selection_cb_fnii, EIF_PROC launch_multiselection_cb_fnpi,
	 EIF_PROC launch_multiunselection_cb_fnpi, EIF_PROC launch_branchopen_cb_fni, EIF_PROC launch_branchclose_cb_fni,
	 EIF_PROC launch_executeleaf_cb_fni, EIF_PROC launch_showrename_cb_fni, EIF_PROC launch_rename_cb_fnis,
	 EIF_PROC launch_noderemoved_cb_fns, EIF_PROC launch_togglevalue_cb_fnii, EIF_PROC launch_highlight_cb_fn,
	 EIF_PROC launch_open_cb_fn, EIF_PROC launch_menuclose_cb_fn, EIF_PROC launch_dragbegin_cb_fnii,
	 EIF_PROC launch_dragdatasize_cb_fns, EIF_PROC launch_dragdata_cb_fnsvi, EIF_PROC launch_dragend_cb_fni,
	 EIF_PROC launch_dropdata_cb_fnsviii, EIF_PROC launch_dropmotion_cb_fniis, EIF_PROC launch_recent_cb_fn,
	 EIF_PROC launch_param_cb_fniv, EIF_PROC launch_flat_action_fn, EIF_PROC launch_flat_button_cb_fniiiis,
	 EIF_PROC launch_flat_focus_cb_fni, EIF_PROC launch_flat_enterwindow_cb_fn, EIF_PROC launch_flat_leavewindow_cb_fn,
	 EIF_PROC launch_draw_cb_fniiiiiic, EIF_PROC launch_height_cb_fni, EIF_PROC launch_hspan_cb_fnii,
	 EIF_PROC launch_mouseclick_cb_fniiiiiis, EIF_PROC launch_mousemotion_cb_fniiiis, EIF_PROC launch_ncols_cb_fn,
	 EIF_PROC launch_nlines_cb_fn, EIF_PROC launch_scrolling_cb_fnii, EIF_PROC launch_vspan_cb_fnii,
	 EIF_PROC launch_width_cb_fni, EIF_PROC launch_cell_fni, EIF_PROC launch_extended_fni,
	 EIF_PROC launch_select_fnii, EIF_PROC launch_switch_fnii, EIF_PROC launch_action_fniiiis,
	 EIF_PROC launch_click_fniis, EIF_PROC launch_colresize_fni, EIF_PROC launch_release_fniis,
	 EIF_PROC launch_resizematrix_fnii, EIF_PROC launch_mousemove_fnii, EIF_PROC launch_enteritem_fnii,
	 EIF_PROC launch_leaveitem_fnii, EIF_PROC launch_scrolltop_fnii, EIF_PROC launch_bgcolor_fniivvv,
	 EIF_PROC launch_fgcolor_fniivvv, EIF_PROC launch_font_fnii, EIF_PROC launch_type_fnii,
	 EIF_PROC launch_dropcheck_fnii, EIF_PROC launch_translatevalue_fniis, EIF_PROC launch_togglevalue_fniii,
	 EIF_PROC launch_drop_fnnii, EIF_PROC launch_menudrop_fnnii, EIF_PROC launch_dropselect_fniinsii,
	 EIF_PROC launch_edition_fniiii, EIF_PROC launch_value_fnii, EIF_PROC launch_value_edit_fniis,
	 EIF_PROC launch_mark_fnii, EIF_PROC launch_markedit_fniii, EIF_PROC launch_flat_motion_cb_fniis,
	 EIF_PROC launch_dropshow_cb_fni, EIF_PROC launch_button_press_cb_fnd, EIF_PROC launch_button_release_cb_fnd,
	 EIF_PROC launch_mousemove_cb_fnd, EIF_PROC launch_change_cb_fnvvv, EIF_PROC launch_drag_cb_fnvvv);

int param_action(Ihandle* dialog, int param_index, void* user_data);

int get_param_dialog (const char* title, void* user_data, const char* format, int param_count, int param_extra, void** param_data);

// One function for each type of event

// Common callbacks
int callback_idle_action(void);

void callback_help_cb(Ihandle *ih);

int callback_map_cb(Ihandle *ih);

int callback_unmap_cb(Ihandle *ih);

int callback_destroy_cb(Ihandle *ih);

int callback_ldestroy_cb(Ihandle *ih);

int callback_getfocus_cb(Ihandle *ih);

int callback_killfocus_cb(Ihandle *ih);

int callback_enterwindow_cb(Ihandle *ih);

int callback_leavewindow_cb(Ihandle *ih);

int callback_k_any(Ihandle *ih, int c);

// Action callbacks
int callback_action_Fn(Ihandle *ih);

// Dialog and progress dialog callback
int callback_close_cb(Ihandle *ih);

int callback_copydata_cb(Ihandle *ih, char* cmdLine, int size);

int callback_dropfiles_cb(Ihandle *ih, char* filename, int num, int x, int y);

int callback_mdiactivate_cb(Ihandle *ih);

int callback_move_cb(Ihandle *ih, int x, int y);

int callback_resize_cb(Ihandle *ih, int width, int height);

int callback_show_cb(Ihandle *ih, int state);

int callback_trayclick_cb(Ihandle *ih, int but, int pressed, int dclick);

// File dialog callback
int callback_file_cb(Ihandle *ih, char* file_name, char* status);

// Color dialog callback
int callback_colorupdate_cb (Ihandle* ih);

// Progress dialog callback
int callback_cancel_cb (Ihandle* ih);
/*
// Get param dialog callback
int callback_getparam(Ihandle* dialog, int param_index, void* user_data);
*/

// Tabs callbacks

int callback_tabchange_cb(Ihandle *ih, Ihandle* new_tab, Ihandle* old_tab);

int callback_tabchangepos_cb(Ihandle *ih, int new_pos, int old_pos);

int callback_tabclose_cb(Ihandle *ih, int pos);

int callback_rightclick_cb(Ihandle *ih, int pos);

// Detach box callback
int callback_detached_cb(Ihandle *ih, Ihandle *new_parent, int x, int y);

int callback_restored_cb(Ihandle *ih, Ihandle *old_parent, int x, int y);

// Expander callbacks
int callback_openclose_cb(Ihandle *ih, int state);

int callback_extrabutton_cb(Ihandle *ih, Ihandle *new_parent, int button, int pressed);

// Val callback
int callback_valuechanged_cb(Ihandle *ih);

// Button callback
int callback_button_cb(Ihandle *ih, int button, int pressed, int x, int y, char* status);

// Canvas callbacks
int callback_action_Fnff(Ihandle *ih, float posx, float posy);
// Also Dialog
int callback_focus_cb(Ihandle *ih, int focus);

int callback_motion_cb(Ihandle *ih, int x, int y, char *status);

int callback_keypress_cb(Ihandle *ih, int c, int press);

int callback_scroll_cb(Ihandle *ih, int op, float posx, float posy);

int callback_wheel_cb(Ihandle *ih, float delta, int x, int y, char *status);

int callback_click_cb(Ihandle *ih, char *url);

// List callbacks
int callback_action_Fnsii(Ihandle *ih, char *text, int item, int state);

int callback_caret_cb(Ihandle *ih, int lin, int col, int pos);

int callback_dblclick_cb(Ihandle *ih, int item, char *text);

int callback_dragdrop_cb(Ihandle *ih, int drag_id, int drop_id, int isshift, int iscontrol);

int callback_dropdown_cb(Ihandle *ih, int state);

int callback_edit_cb(Ihandle *ih, int c, char *new_value);

int callback_multiselect_cb(Ihandle *ih, char *value);

// Spin callback
int callback_spin_cb(Ihandle *ih, int inc);

// Text callback
int callback_action_Fnis(Ihandle *ih, int c, char *new_value);

// Toggle callback
int callback_action_Fni(Ihandle* ih, int state);

// Tree callbacks
int callback_selection_cb (Ihandle *ih, int id, int status);

int callback_multiselection_cb (Ihandle *ih, int* ids, int n);

int callback_multiunselection_cb (Ihandle *ih, int* ids, int n);

int callback_branchopen_cb (Ihandle *ih, int id);

int callback_branchclose_cb (Ihandle *ih, int id);

int callback_executeleaf_cb (Ihandle *ih, int id);

int callback_showrename_cb (Ihandle *ih, int id);

int callback_rename_cb (Ihandle *ih, int id, char *title);

int callback_noderemoved_cb (Ihandle *ih, void* userdata);

int callback_togglevalue_cb_Fnii (Ihandle *ih, int id, int state);

// Menu callbacks
int callback_highlight_cb (Ihandle *ih);

int callback_open_cb (Ihandle *ih);

int callback_menuclose_cb (Ihandle *ih);

// Drag&Drop callbacks
int callback_dragbegin_cb (Ihandle* ih, int x, int y);

int callback_dragdatasize_cb (Ihandle* ih, char* type);

int callback_dragdata_cb (Ihandle* ih, char* type, void* data, int size);

int callback_dragend_cb (Ihandle* ih, int action);

int callback_dropdata_cb (Ihandle* ih, char* type, void* data, int size, int x, int y);

int callback_dropmotion_cb (Ihandle *ih, int x, int y, char *status);

// Menu recent files
int callback_recent_cb (Ihandle *ih);

void config_recent_init (Ihandle* ih, Ihandle* menu, int max_recent);

// Flat button
int callback_flat_action_Fn(Ihandle *ih);

int callback_flat_button_cb(Ihandle *ih, int button, int pressed, int x, int y, char* status);

int callback_flat_focus_cb(Ihandle *ih, int focus);

int callback_flat_enterwindow_cb(Ihandle *ih);

int callback_flat_leavewindow_cb(Ihandle *ih);

// Cells
int callback_draw_cb(Ihandle* ih, int line, int column, int xmin, int xmax, int ymin, int ymax, cdCanvas* canvas);

int callback_height_cb(Ihandle* ih, int line);

int callback_hspan_cb(Ihandle* ih, int line, int column);

int callback_mouseclick_cb(Ihandle* ih, int button, int pressed, int line, int column, int x, int y, char* status);

int callback_mousemotion_cb(Ihandle *ih, int line, int column, int x, int y, char *r);

int callback_ncols_cb(Ihandle* ih);

int callback_nlines_cb(Ihandle* ih);

int callback_scrolling_cb(Ihandle* ih, int line, int column);

int callback_vspan_cb(Ihandle* ih, int line, int column);

int callback_width_cb(Ihandle* ih, int column);

// Color bar

char* callback_cell_cb(Ihandle* ih, int cell);

int callback_extended_cb(Ihandle* ih, int cell);

int callback_select_cb(Ihandle* ih, int cell, int type);

int callback_switch_cb(Ihandle* ih, int prim_cell, int sec_cell);

// Matrix
int callback_action_Fniiiis(Ihandle *ih, int key, int lin, int col, int edition, char* value);

int callback_click_cb_Fniis (Ihandle *ih, int lin, int col, char *status);

int callback_colresize_cb (Ihandle *ih, int col);

int callback_release_cb (Ihandle *ih, int lin, int col, char *status);

int callback_resizematrix_cb (Ihandle *ih, int width, int height);

int callback_mousemove_cb (Ihandle *ih, int lin, int col);

int callback_enteritem_cb (Ihandle *ih, int lin, int col);

int callback_leaveitem_cb (Ihandle *ih, int lin, int col);

int callback_scrolltop_cb (Ihandle *ih, int lin, int col);

int callback_bgcolor_cb (Ihandle *ih, int lin, int col, int *red, int *green, int *blue);

int callback_fgcolor_cb (Ihandle *ih, int lin, int col, int *red, int *green, int *blue);

char* callback_font_cb (Ihandle* ih, int lin, int col);

char* callback_type_cb (Ihandle* ih, int lin, int col);

int callback_dropcheck_cb (Ihandle *ih, int lin, int col);

char* callback_translatevalue_cb (Ihandle* ih, int lin, int col, char* value);

int callback_togglevalue_cb_Fniii (Ihandle *ih, int lin, int col, int status);

int callback_drop_cb (Ihandle *ih, Ihandle *drop, int lin, int col);

int callback_menudrop_cb (Ihandle *ih, Ihandle *drop, int lin, int col);

int callback_dropselect_cb (Ihandle *ih, int lin, int col, Ihandle *drop, char *t, int i, int v);

int callback_edition_cb (Ihandle *ih, int lin, int col, int mode, int update);

char* callback_value_cb (Ihandle* ih, int lin, int col);

int callback_value_edit_cb (Ihandle *ih, int lin, int col, char* newval);

int callback_mark_cb (Ihandle* ih, int lin, int col);

int callback_markedit_cb (Ihandle *ih, int lin, int col, int marked);

// Flat tabs

int callback_flat_motion_cb(Ihandle *ih, int x, int y, char *status);

// Drop button

int callback_flat_dropshow_cb (Ihandle *ih, int state);

// Dial

int callback_button_press_cb (Ihandle *ih, double angle);

int callback_button_release_cb (Ihandle *ih, double angle);

int callback_mousemove_cb_Fnd (Ihandle *ih, double angle);

// Color browse

int callback_change_cb (Ihandle *ih, unsigned char r, unsigned char g, unsigned char b);

int callback_drag_cb (Ihandle *ih, unsigned char r, unsigned char g, unsigned char b);

// Set callbacks

void connect_function (const char *name, int operation);

void connect_callback (Ihandle* ih, const char *name, const char *type, int operation);

#endif
